
clear all
	
cd "${data}"	

use "d1.dta", clear

drop if IMD5000_==.
gen key_coalition = election+"_"+string(IMD5000_)
*save "data2.dta", replace

merge m:1 key_coalition using "coalition.dta"
drop _merge 
*this brings the following variables: coalitiongovernment and ingovernment


merge m:1 election using "parlgov_coded.dta"
*drop _merge


* if merge==1 , correct these that are not matched: 

replace daysbetween = 29 if election=="BELF1999"
replace daysbetween = . if election=="BELF2019"
replace daysbetween = 29 if election=="BELW1999"
replace daysbetween = . if election=="BELW2019"
replace daysbetween = 30 if election=="DEU12002"
replace daysbetween = 59 if election=="USA_1996" // cabinet formation 3 january 1997, election day 5 november 1996
replace daysbetween = 62 if election=="USA_2004" // cabinet formation 3 january 2005, election day 2 november 2004
replace daysbetween = 60 if election=="USA_2008" // cabinet formation 3 january 2009, election day 4 november 2008
replace daysbetween = 58 if election=="USA_2012" // cabinet formation 3 january 2013, election day 6 november 2012
replace daysbetween = 56 if election=="USA_2016" // cabinet formation 3 january 2017, election day 8 november 2016
replace daysbetween = 61 if election=="USA_2020" // cabinet formation 3 january 2021, election day 3 november 2020

replace daysbetween = 2 if election=="GRC12015"
replace daysbetween = 1 if election=="GRC22015"


*drop if daysbetween == . 


drop _merge

encode election, gen(election_)
encode country, gen(country_)

gen daysaftercabinetformation = daysafter - daysbetween // if negative, cabinet hasnt formed yet and R is interviewed before a cabinet is formed, if positive, cabinet has formed and R is interviewed after a cabinet is formed.


label define ingovernmentlabel 1 "in government" 0 "in opposition"
label values ingovernment ingovernmentlabel  
label define coalitiongovernmentlabel 0"not a coalition" 1"coalition" 
label values coalitiongovernment coalitiongovernmentlabel
label variable IMD5012_ "expert perception"
label variable IMD3007_ "voter perception"
label variable daysaftercabinetformation "days since cabinet"
label variable daysafter "days since election"
label variable country_ "country"
label variable election_ "election"
label variable IMD5012_ "party"



replace election = "BELF_1999" if election=="BELF1999"
replace election = "BELF_2019" if election=="BELF2019"
replace election = "BELW_1999" if election=="BELW1999"
replace election = "BELW_2019" if election=="BELW2019"
replace election = "DEU_2002" if election=="DEU12002"

save "d2.dta", replace


